/**
 * 防抖
 * @param fn 需要运行的函数
 * @param delay 等待时长
 * @returns {(function(): void)|*}
 */
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        if (timer !== null) {
            clearTimeout(timer);
        }
        timer = setTimeout(fn, delay);
    };
};

/**
 * 节流
 * @param fn 需要运行的函数
 * @param delay 等待时长
 * @returns {(function(*): void)|*}
 */
const throttle = (fn, delay) => {
    let valid = true;
    return function () {
        if (!valid) {
            return false;
        }
        valid = false;
        setTimeout(() => {
            fn();
            valid = true;
        }, delay);
    };
};
module.exports = {
    throttle,
    debounce
}